import { 
  Modal  
} from '@arco-design/web-react';

export function confirmDelete(message:string, onOk:(e?: MouseEvent) => Promise<any> | void){
  Modal.confirm({
    title: '确定删除？',
    content: message,
    okText: '删除',
    okButtonProps: {
      status: 'danger',
    },
    onOk: onOk
  });
}

export function formatDateTime(timestamp) {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = addZero(date.getMonth() + 1);
    const day = addZero(date.getDate());
    const hours = addZero(date.getHours());
    const minutes = addZero(date.getMinutes());
    const seconds = addZero(date.getSeconds());
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    // return year + '-' + month + '-' + day;
}

//加0
function addZero(num:number) {
    return num < 10 ? '0' + num : num;
}

export function isLogin() {
    const token = sessionStorage.getItem('token');
    console.log('token', token);
    if (token) {
        return true;
    }
    return false;
}

export function print(){
    var iframe=document.getElementById("print-iframe");
    if(!iframe){  
      iframe = document.createElement('IFRAME');
      
      iframe.setAttribute("id", "print-iframe");
      iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
      document.body.appendChild(iframe);
    }
    var el = document.getElementById("printArea");

    var doc = iframe.contentWindow.document;
    //这里可以自定义样式
    doc.write(`<style media="print">
      @page {size: auto;margin: 0mm;}
      .no-print {display: none} 
      table {width: 99%; border: solid thin #ccc; border-spacing:0; margin-top: 10px} 
      th {border:solid thin #ccc ;} 
      td {border:solid thin #ccc ;} 
      #print-container {margin: 0.5in 0.4in 0.5in 0.6in }
      
    </style>`); //解决出现页眉页脚和路径的问题
    doc.write('<div id="print-container">' + el.innerHTML + '</div>');
    doc.close();
    iframe.contentWindow.focus();
    
    setTimeout(function(){ iframe.contentWindow.print();},50)  //解决第一次样式不生效的问题
}

export function removeExt(filename:string):string{
  let off = filename.lastIndexOf(".");
  if(off>0){
   return filename.substring(0,off)
  }
  return filename;
}

export function perView(val:number):string{
  return  Math.round(1000*val)/10 + '%'
}


export function floatView(val:number):number{
  return  Math.round(10*val)/10
}

export function isExpire():boolean{
  let target = new Date("2033-09-13T07:24:00")

  if(target.getTime()>Date.now()){
      return false
  }else{
      return true
  }
}
